<?php
import("ORG.Util.Page"); //分页类
import("@.ORG.Url");
class PhotoAction extends Action {
    //相册简介
    public function index(){
        header("Content-Type:text/html; charset=utf-8");
        $file = M("file");
        //相册分页
        $count=$file->order("f_date desc")->count();           //总共的条数
        $Page = new Page($count, PAGE_NUMBER);
        $show = $Page->show();     //分页显示输出
        $listcount=$file->limit($Page->firstRow.','.$Page->listRows)->order("f_date desc")->select();

        // 验证添加权限
        $isHasInsert = UserModel::isRoles(UserModel::USER_INSERT);

        $this->assign("isHasInsert",$isHasInsert);
        $this->assign("page",$show);
        $this->assign("listcount",$listcount);
        $this->display();
    }

    public  function PhotoAdd(){
        $this->display();
    }
    public function add_xc(){
        header("Content-Type:text/html; charset=utf-8");

        $this->display();
    }

    //添加相册
    public function add_xc1(){
        header("Content-Type:text/html; charset=utf-8");

        // 验证添加权限
        $isHasInsert = UserModel::isRoles(UserModel::USER_INSERT);
        if (empty($isHasInsert)) {
            $this->error('您没有添加权限，如有问题，请咨询管理员！');
        }

        $file = M("file");
        //匹配该文件夹是否存在
        $isDir = $file->where("f_dir='" . $this->_post('xcname') ."'")->limit(1)->find();

        if (empty($isDir))
        {
            if($this->_post('xcname'))
            {
                $realDir = PhotoModel::getRealDir();

                $data = array(
                    'f_dir' => $this->_post('xcname'),
                    'f_content' => $this->_post('xcshow'),
                    'f_date' => time(),
                    'u_id' => session('u_id'),
                    'f_real_dir' => $realDir,
                    );

                $result = $file->add($data);

                if ($result)
                {
                    //创建相册
                    createFolder("../public/upload/" . $realDir);

                    $this->success($this->_post('xcname') . "创建成功！");
                }
                else
                {
                    $this->error("创建失败，请与管理人员联系！");
                }
            }
            else
            {
                $this->error("亲，没填写文件夹哟！");
            }
       }
       else
       {
          $this->error("亲，该文件夹已经创建了！");
       }
    }

    //相片显示
    public function PhotoNum(){
        $photo = M();
        //获得记录数
        $count = $photo->table("tp_file f,tp_photo o")->where("o.f_id='".$this->_get('f_id')."' and o.f_id=f.f_id")->limit(1)->count();
        $Page = new Page($count, PAGE_NUMBER);  //分页条数
        $show = $Page->show();       //分页显示输出
        $result = $photo->table("tp_file f,tp_photo o")->where("o.f_id='".$this->_get('f_id')."' and o.f_id=f.f_id")->limit($Page->firstRow.','.$Page->listRows)->order("o.p_date desc ")->select();

        // 验证添加权限
        $isHasInsert = UserModel::isRoles(UserModel::USER_INSERT);

        $this->assign("isHasInsert", $isHasInsert);
        $this->assign("f_id",$this->_get('f_id'));
        $this->assign("result",$result);
        $this->assign("page",$show);
        $this->display();
    }

    //添加相片页面
    public function add_zp()
    {
        $file = M("file");
        $this->assign("f_id",$this->_get('f_id'));
        $this->display();
    }

    //处理添加照片页面
    public function Deal_zp()
    {
        // 验证添加权限
        $isHasInsert = UserModel::isRoles(UserModel::USER_INSERT);

        if (empty($isHasInsert)) {
            $this->error("您没有添加权限，如有问题，请咨询管理员！");
        }

        //上传功能
        import("@.ORG.UploadFile");

        //照片表对象
        $photo = M("photo");

        //获取相册的名称
        $album = M("file")->field("f_id,f_dir,f_real_dir")->where("f_id='" . $this->_post('xcname') . "'")->limit(1)->find();

        $upload = new UploadFile();
        $upload->maxSize = 3292200;
        $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');
        $upload->savePath = "../public/upload/".$album['f_real_dir']."/";
        $upload->thumb = true;

        $upload->thumbPrefix = 'm_,s_';  //生产1张缩略图
        $upload->thumbMaxWidth = '250,80';
        $upload->thumbMaxHeight = '200,80';

        if (!$upload->upload())
        {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        }
        else
        {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();

            $data = array(
                'f_id' => $this->_post('xcname'),
                'p_content' => $this->_post('zpshow'),
                'p_date' => time(),
                'p_image' => $uploadList[0]['savename'],
                'u_id' => session('u_id'),
                );

            $result = $photo->add($data);

            if ($result)
            {
                $this->success("照片添加成功！");
            }
            else
            {
                $this->error("照片添加失败，请与管理员联系！");
            }
        }
    }
}
?>